package com.xingxue.logistics.controller.sys;

import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.Page;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.common.vo.TreeNode;
import com.xingxue.logistics.dao.domain.SysUser;
import com.xingxue.logistics.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author zgx at 2018/10/10 0010 上午 10:46
 */

@RestController
@RequestMapping("sys/user")
@Api(description = "用户相关控制器")
public class UserController {

    @Autowired
    UserService userService;




    @ApiOperation(value = "分页查询用户菜单")
    @GetMapping("page")
    public ResponseEntity<RestResult<PageInfo<SysUser>>> search(@RequestParam(required = false) @ApiParam(value = "查询条件：用户ID") Integer id,
                                                                @RequestParam(required = false) @ApiParam("查询条件：用户昵称") String nick,
                                                                @RequestParam(required = false) @ApiParam("查询条件：用户账号") String account,
                                                                @RequestParam(required = false,defaultValue = "1") @ApiParam(value = "当前页码",defaultValue = "1") Integer page,
                                                                @RequestParam(required = false,defaultValue = "10") @ApiParam(value="每页行数",defaultValue = "10") Integer rows) {
        PageInfo<SysUser> userPage = userService.searchPage(id, nick, account,page,rows);
        return ResponseEntity.ok(new RestResult<>(true,"数据加载完成",userPage));
    }

    @ApiOperation(value = "分页查询用户菜单(easyUI用)")
    @GetMapping("page_easyui")
    public ResponseEntity<Page<SysUser>> searchEasyUI(@RequestParam(required = false) @ApiParam(value = "查询条件：用户ID") Integer id,
                                                      @RequestParam(required = false) @ApiParam("查询条件：用户昵称") String nick,
                                                      @RequestParam(required = false) @ApiParam("查询条件：用户账号") String account,
                                                      @RequestParam(required = false,defaultValue = "1") @ApiParam(value = "当前页码",defaultValue = "1") Integer page,
                                                      @RequestParam(required = false,defaultValue = "10") @ApiParam(value="每页行数",defaultValue = "10") Integer rows) {
        PageInfo<SysUser> userPage = userService.searchPage(id, nick, account,page,rows);
        return ResponseEntity.ok(new Page(userPage));
    }

    @ApiOperation("新增用户")
    @PostMapping
    public ResponseEntity<RestResult> addUser(@RequestBody SysUser user){
        RestResult result = userService.add(user);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "修改用户",notes = "依据用户ID进行修改")
    @PutMapping
    public ResponseEntity<RestResult> updateUser(@RequestBody SysUser user){
        RestResult result = userService.update(user);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "删除菜单",notes = "根据用户ID删除")
    @DeleteMapping("{id}")
    public ResponseEntity<RestResult> deleteUser(@PathVariable("id") @ApiParam("需要删除的用户ID") Integer id){
        RestResult result = userService.delete(id);
        return ResponseEntity.ok(result);
    }

}
